package janino;

import org.codehaus.janino.ClassBodyEvaluator;
public class DynamicClassExample {
    public static void main(String[] args) throws Exception {
        // Create an instance of ClassBodyEvaluator
        ClassBodyEvaluator cbe = new ClassBodyEvaluator();
        
        // Set the name of the generated class
        cbe.setClassName("DynamicClass");
        
        // Set the extended class for the generated class
        cbe.setExtendedClass(SuperClass.class);
        
        // Define the dynamic class contents
        String classContents = 
        " public DynamicClass(){}\n" +
                " public void printMessage() {" +
        "System.out.println(\"Dynamically generated code\");" +
        "}";
        
        // Compile the dynamic class
        cbe.cook("DynamicClass.java",classContents);
        
        // Instantiate the dynamic class
        SuperClass instance = (SuperClass) cbe.getClazz().getConstructor().newInstance();
        
        // Invoke a method on the dynamic class
        instance.printMessage(); // Calls the method from the dynamically generated class
    }

}